/*
 * This file is part of the PDF Split And Merge source code
 * Created on 24 ott 2015
 * Copyright 2017 by Sober Lemur S.r.l. (info@soberlemur.com).
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.pdfsam.service.news;

import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.inject.Inject;
import jakarta.inject.Named;
import org.pdfsam.core.AppBrand;
import org.pdfsam.core.BrandableProperty;
import org.pdfsam.model.news.NewsData;
import org.pdfsam.persistence.PersistenceException;
import org.pdfsam.persistence.PreferencesRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.net.URL;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import static org.pdfsam.i18n.I18nContext.i18n;
import static org.sejda.commons.util.RequireUtils.requireNotNullArg;

/**
 * Default JSON implementation of a news service
 *
 * @author Andrea Vacondio
 */
public class DefaultNewsService implements NewsService {

    private static final Logger LOG = LoggerFactory.getLogger(DefaultNewsService.class);
    static final String LATEST_NEWS_ID = "latest.news.id";
    static final String LATEST_IMPORTANT_NEWS_ID = "latest.important.news.id";
    private final PreferencesRepository repo;
    private final AppBrand appBrand;
    private final ObjectMapper mapper;

    @Inject
    DefaultNewsService(AppBrand appBrand, ObjectMapper mapper, @Named("newsRepository") PreferencesRepository repo) {
        requireNotNullArg(appBrand, "Application info cannot be null");
        this.appBrand = appBrand;
        this.mapper = mapper;
        this.repo = repo;
    }

    @Override
    public List<NewsData> getLatestNews() {
        try {
            return Arrays.asList(
                    mapper.readValue(new URL(appBrand.property(BrandableProperty.NEWS_URL)), NewsData[].class));
        } catch (IOException e) {
            LOG.warn(i18n().tr("Unable to retrieve latest news"), e);
        }
        return Collections.emptyList();
    }

    @Override
    public int getLatestNewsSeen() {
        return this.repo.getInt(LATEST_NEWS_ID, -1);
    }

    @Override
    public void setLatestNewsSeen(int id) {
        try {
            this.repo.saveInt(LATEST_NEWS_ID, id);
            LOG.trace("Latest news id stored");
        } catch (PersistenceException e) {
            LOG.error("Unable to store the id of the most recently seen news");
        }
    }

    @Override
    public int getLatestImportantNewsSeen() {
        return this.repo.getInt(LATEST_IMPORTANT_NEWS_ID, -1);
    }

    @Override
    public void setLatestImportantNewsSeen(int id) {
        try {
            this.repo.saveInt(LATEST_IMPORTANT_NEWS_ID, id);
            LOG.trace("Latest important news id stored");
        } catch (PersistenceException e) {
            LOG.error("Unable to store the id of the most recently seen important news");
        }
    }

    @Override
    public void clear() {
        try {
            this.repo.clean();
        } catch (PersistenceException e) {
            LOG.error("Unable to clear latest news store", e);
        }
    }
}
